﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QL_tuyensinh
{
    public partial class frmGIAODIENDANGNHAP : DevExpress.XtraEditors.XtraForm
    {
        public static string stringConn = "";

        public frmGIAODIENDANGNHAP()
        {
            InitializeComponent();
        }
        private void QUANLITUYENSINH_Load(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            timer1.Start();
            var kn = new Ketnoi(stringConn);
            dataGridView1.DataSource = kn.ExcuteQuery("select * from username ");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = 10;
            lbtieude.Left += 1;
            if (lbtieude.Left > this.Width - lbtieude.Width || lbtieude.Left <= 0)
                i = -1;
       }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            /*
            SqlConnection conn = new SqlConnection();
            try {
                
            DataSet dset = new DataSet();
            SqlDataAdapter sqlDAdapter = new SqlDataAdapter("SELECT * FROM USERS",conn);// WHERE nguoidung = '" + textBox1.Text.ToString() + "' and pass = '" + textBox2.Text.ToString() + "'", conn);
            sqlDAdapter.Fill(dset);
            this.dataGridView1.DataSource = dset.Tables[0];
               
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (tbnguoidung.Text == dataGridView1.Rows[i].Cells[1].Value.ToString() && dataGridView1.Rows[i].Cells[2].Value.ToString() == tbmatkhau.Text && dataGridView1.Rows[i].Cells[3].Value.ToString() == "0")
                    {
                        frmCHUONGTRINH ct = new frmCHUONGTRINH();
                        this.Hide();
                        ct.ShowDialog();
                        this.Show();
                        tbnguoidung.Text = "";
                        tbmatkhau.Text = "";
                    }
                    else if (tbnguoidung.Text == dataGridView1.Rows[i].Cells[1].Value.ToString() &&
                        dataGridView1.Rows[i].Cells[2].Value.ToString() == tbmatkhau.Text &&
                        dataGridView1.Rows[i].Cells[3].Value.ToString() == "1")
                    {
                        frmgiaodienquantri qt = new frmgiaodienquantri();
                        this.Hide();
                        qt.ShowDialog();
                        this.Show();
                        tbnguoidung.Text = "";
                        tbmatkhau.Text = "";
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("không lấy được dữ liệu!" + ex.Message);
            }
             */
            /*
            SqlConnection conn = new SqlConnection(frmGIAODIENDANGNHAP.stringConn);
            conn.Open();

            try {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if(tbnguoidung.Text.ToString() == dataGridView1.Rows[i].Cells[0].Value.ToString() && dataGridView1.Rows[i].Cells[1].Value.ToString() == tbmatkhau.Text.ToString() && dataGridView1.Rows[i].Cells[2].Value.ToString() == "0")
                    {
                        frmCHUONGTRINH ct = new frmCHUONGTRINH();
                        this.Hide();
                        ct.ShowDialog();
                        this.Show();
                        tbnguoidung.Text = "";
                        tbmatkhau.Text = "";
                    }
                    else  if(tbnguoidung.Text == dataGridView1.Rows[i].Cells[0].Value.ToString() && dataGridView1.Rows[i].Cells[1].Value.ToString() == tbmatkhau.Text && dataGridView1.Rows[i].Cells[2].Value.ToString() == "1")
                    {
                        frmgiaodienquantri qt = new frmgiaodienquantri();
                        this.Hide();
                        qt.ShowDialog();
                        this.Show();
                  //      tbnguoidung.Text = "";
                  //      tbmatkhau.Text = "";
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("không lấy được dữ liệu!" + ex.Message);
            }
             */
            SqlConnection conn = new SqlConnection(frmGIAODIENDANGNHAP.stringConn);
            conn.Open();
            if (tbnguoidung.Text == "" && tbmatkhau.Text == "")
            {
                MessageBox.Show("Vui lòng nhập thông tin đăng nhập!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (tbnguoidung.Text == "admin" && tbmatkhau.Text != "admin")
            {
                MessageBox.Show("Mật khẩu đăng nhập sai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbnguoidung.Text = "admin";
                tbmatkhau.Text = "";
                tbmatkhau.Focus();
            }
            else if (tbmatkhau.Text == "admin" && tbnguoidung.Text != "admin")
            {
                MessageBox.Show("Tài khoản đăng nhập sai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbnguoidung.Text = "";
                tbmatkhau.Text = "";
                tbnguoidung.Focus();
            }
            else if (tbnguoidung.Text != "admin" && tbmatkhau.Text != "admin")
            {
                MessageBox.Show("Tài khoản hoặc mật khẩu không đúng. Vui lòng nhập lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbnguoidung.Text = "";
                tbmatkhau.Text = "";
                tbnguoidung.Focus();
            }
            else if (tbnguoidung.Text == "admin" && tbmatkhau.Text == "admin")
            {
                MessageBox.Show("Bạn đã Đăng nhập thành công!", "Thông báo", MessageBoxButtons.OK);
                this.Hide();
                FRM_CHON_CHUC_NANG f = new FRM_CHON_CHUC_NANG();
                this.Hide();
                f.ShowDialog();
                
            }            

}
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(frmGIAODIENDANGNHAP.stringConn);

            try
            {

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (tbnguoidung.Text == dataGridView1.Rows[i].Cells[1].Value.ToString() && dataGridView1.Rows[i].Cells[2].Value.ToString() == tbmatkhau.Text && dataGridView1.Rows[i].Cells[3].Value.ToString() == "0")
                    {
                        frmCHUONGTRINH ct = new frmCHUONGTRINH();
                        this.Hide();
                        ct.ShowDialog();
                        this.Show();
                        tbnguoidung.Text = "";
                        tbmatkhau.Text = "";
                    }
                    else if (tbnguoidung.Text == dataGridView1.Rows[i].Cells[1].Value.ToString() &&
                        dataGridView1.Rows[i].Cells[2].Value.ToString() == tbmatkhau.Text &&
                        dataGridView1.Rows[i].Cells[3].Value.ToString() == "1")
                    {
                        frmgiaodienquantri qt = new frmgiaodienquantri();
                        this.Hide();
                        qt.ShowDialog();
                        this.Show();
                        tbnguoidung.Text = "";
                        tbmatkhau.Text = "";
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("không lấy được dữ liệu!" + ex.Message);
            }
        }


       
    }
}
